package top.livepan.system.controller;

import com.livepan.server.BaseController;
import com.livepan.server.error.BusinessException;
import com.livepan.server.response.CommonReturnType;
import com.livepan.server.service.RoleUserService;
import com.livepan.server.service.model.RoleUserModel;
import com.livepan.server.service.model.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/roleUser")
public class RoleUserController extends BaseController {

    public static final String BUSINESS_NAME = "角色用户关联";

    private static final Logger LOG = LoggerFactory.getLogger(RoleUserController.class);

    @Autowired
    private RoleUserService roleUserService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonReturnType roleUser(@RequestParam String userId) {
        List<RoleUserModel> roleUserModelList = roleUserService.list(userId);
        return CommonReturnType.create(roleUserModelList);
    }

    @ResponseBody
    @RequestMapping("/saveRoleUser")
    public CommonReturnType saveRoleUser(@RequestParam String userId, @RequestBody List<String> roleIdList) throws BusinessException {
        roleUserService.clear(userId);

        roleUserService.save(userId, roleIdList);
        return CommonReturnType.create(null);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public CommonReturnType delete(@PathVariable String id) {
        roleUserService.delete(id);
        return CommonReturnType.create(null);
    }
}
